﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weight.Model;
using System.Data;
using Dapper;
using Weight.Base;

namespace Weight.Dal
{
    public class DalRole
    {
        /// <summary>
        /// 查詢所有角色
        /// </summary>
        /// <returns></returns>
        public IEnumerable<RoleEntity> QueryAll (string name="")
        {
            using(IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = @"select * from Role ";
                if(!string.IsNullOrEmpty(name))
                {
                    name = "%" + name + "%";
                    sql += " where RoleName like @name";
                }
               return   conn.Query<RoleEntity>(sql,new { name });
            }
        }

        /// <summary>
        /// 通过名字查询角色
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public IEnumerable<RoleEntity> QueryByName(string name)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = " select * from Role where RoleName=@name";
                return conn.Query<RoleEntity>(sql,new { name});
            }
        }


        /// <summary>
        /// 通过名字查询角色
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public IEnumerable<RoleEntity> QueryByName(string name,string noThisId)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = " select * from Role where RoleName=@name and Id<>@Id";
                return conn.Query<RoleEntity>(sql, new { name,Id= noThisId });
            }
        }


        /// <summary>
        /// 通过名字查询角色
        /// </summary>
        /// <param name="id">角色编号</param>
        /// <returns></returns>
        public IEnumerable<RoleEntity> QueryById(string id)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = " select * from Role where Id=@id";
                return conn.Query<RoleEntity>(sql, new { id });
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Add(RoleEntity entity)
        {
            using(IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = @"insert into Role(Id,RoleName,Simplicity,Memo,CreatDate,Creator)values(@Id,@RoleName,@Simplicity,@Memo,@CreatDate,@Creator)";
                return conn.Execute(sql, entity) > 0 ? true : false;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(RoleEntity entity)
        {
            using(IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = " update Role set RoleName=@RoleName,Simplicity=@Simplicity, Memo=@Memo,CreatDate=@CreatDate,Creator=@Creator where Id=@Id";
                return conn.Execute(sql, entity) > 0 ? true : false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool Delete(string Id)
        {
            using(IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = " delete from Role where Id=@Id";
                return conn.Execute(sql, new { Id }) > 0 ? true : false;
            }
        }
    }
}
